<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .content {
      height: 300px;
      overflow: auto;
    }
    li {
      line-height: 150px;
      text-align: center;
    }
  </style>
  <body>
    <input type="text" />
    <div class="content">
      <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
      </ul>
    </div>
  </body>
</html>
<script>
  // 函数的防抖 是在 n 秒后执行，如果在 n 秒内又触发了事件，则会重新计算函数执行时间。
  let dom = document.querySelector("input");
  let time = null;
  dom.oninput = function (e) {
    if (time) {
      clearTimeout(time);
    }
    time = setTimeout(() => {
      console.log(e.target.value);
    }, 2000);
  };

  let scrollDom = document.querySelector(".content");
  // 函数的节流，// 使得一定时间内只触发一次函数。原理是通过判断是否到达一定时间来触发函数。
  let clearTime = null;
  scrollDom.onscroll = function (e) {
    if (clearTime) {
      return;
    }
    clearTime = setTimeout(() => {
      scrollDom.style.background =
        "#" + Math.random().toString(16).substr(2, 6);
      clearTime = null;
    }, 1000);
  };

  // let obj = {
  //   name: "张三",
  //   sing: function () {
  //     console.log(this);
  //     setTimeout(
  //       function () {
  //         console.log(this);
  //       }.bind(this),
  //       1000
  //     );
  //   },
  // };

  // obj.sing();

  class Tab {
    constructor() {
      this.num = 20;
      this.el = document.querySelector("input");
      this.el.oninput = this.input;
    }
    input = () => {
      console.log(this.num);
    };
  }

  class Chlid extends Tab {
    constructor() {
      super();
      this.number = 30;
      this.el.onblur = this.blur.bind(this);
    }
    blur() {
      console.log("我是去焦点了");
    }
  }

  new Chlid();
</script>
